<?

include($_SERVER['DOCUMENT_ROOT'].'/includes/functions.inc.php');
include($_SERVER['DOCUMENT_ROOT'].'/config.php');
include($_SERVER['DOCUMENT_ROOT'].'/groups/database.php');
include($_SERVER['DOCUMENT_ROOT'].'/filter.libs.php');

portal_start();

$page_title = "Statistics";
include($_SERVER['DOCUMENT_ROOT'].'/header.inc.php');



        if (!empty($row['excerpt'])) {
                print "<p><b>".he($row['excerpt'])."</b>";
        }
        if (!empty($row['description'])) {
                print "<div style=\"padding:5px;margin:5px;background-color:#eeeeee;font-size:1.1em\">".MakeLinks(he($row['description']))."</div>";
        }


#########################
# Setup Defaults


$sql = array();
$sql['columns'] = '*';
$sql['tables'] = array();

$sql['tables'][] = "p2.gridimage{$id}_bi_view AS gi";
$sql['tables']['cc'] = "LEFT JOIN geograph_live.category_canonical AS cc USING (imageclass)";

$sql['wheres'] = array();

$sql['limit'] = 25;


#########################
# html form

	print "<h2>Statistics for images in the ".he($row['title'])." Portal</h2>";

print "<form class=\"sform\">";

include($_SERVER['DOCUMENT_ROOT'].'/filter_setup.inc.php');

print "</form>";

#########################
# Display results!

if (empty($_SERVER['QUERY_STRING'])) {
	print "<p>Total images: <b>".number_format($row['count'])."</b> <b>(may be a subset of the available images on geograph during testing)</b></p>";

} else {
	$count_query = sqlMakeCountQuery($sql);
	
	$resultCount = getOne($count_query);
	
	print "<p>Total images: <b>".number_format($resultCount)."</b></p>";
}

#############

$sql['columns'] = array();
$sql['columns'][] = 'COUNT(DISTINCT grid_reference) AS gridrefs';
$sql['columns'][] = "COUNT(DISTINCT concat(substring(gi.grid_reference,1,length(gi.grid_reference)-3),substring(gi.grid_reference,length(gi.grid_reference)-1,1))) AS hectads"; //TODO BI specific!
$sql['columns'][] = "COUNT(DISTINCT SUBSTRING(gi.grid_reference,1,3 - reference_index)) AS myriads"; //TODO BI specific!
$sql['columns'][] = "COUNT(DISTINCT user_id) AS contributors"; 
$sql['columns'][] = "COUNT(DISTINCT YEAR(imagetaken)) AS years"; 
$sql['columns'][] = "COUNT(DISTINCT imagetaken) AS days"; 
$sql['columns'][] = "COUNT(DISTINCT imageclass) AS categories"; 
$sql['columns'][] = "COUNT(DISTINCT canonical) AS canonicals"; 

$count_query = sqlMakeQuery($sql);
	
$results = getRow($count_query);


print "<table border=1 cellspacing=0 cellpadding=10>";
#print "<tr><th>Number of<br/>Different</th><th>Matric</th><th></th></tr>";

#############

print "<tr><td align=right><b>".number_format($results['gridrefs'])."</b></td><td>Gridsquares (1km squares)</td><td><a href=\"/breakdown.php?by=gridref\">view breakdown</a></td></tr>";

if ($row['gridsquares']) {
	print "<tr><td colspan=3>(of an estimated <b>".number_format($row['gridsquares'])."</b> squares, so this portal has approximately <b>".intval($results['gridrefs']/$row['gridsquares']*100)."%</b> coverage)</td></tr>";
}


#############

print "<tr><td align=right><b>".number_format($results['hectads'])."</b></td><td>Hectads (10km squares)</td><td><a href=\"/breakdown.php?by=hectad\">view breakdown</a></td></tr>";

#############

print "<tr><td align=right><b>".number_format($results['myriads'])."</b></td><td>Myriads (100km squares)</td><td><a href=\"/breakdown.php?by=myriad\">view breakdown</a></td></tr>";

#############

print "<tr><td align=right><b>".number_format($results['contributors'])."</b></td><td>Contributors</td><td><a href=\"/breakdown.php?by=user_id\">view breakdown</a></td></tr>";

#############

print "<tr><td align=right><b>".number_format($results['years'])."</b></td><td>Years with photos</td><td><a href=\"/breakdown.php?by=year\">view breakdown</a></td></tr>";

#############

print "<tr><td align=right><b>".number_format($results['days'])."</b></td><td>Days with photos</td><td><a href=\"/breakdown.php?by=taken\">view breakdown</a></td></tr>";

#############

print "<tr><td align=right><b>".number_format($results['categories'])."</b></td><td>Categories</td><td><a href=\"/breakdown.php?by=category\">view breakdown</a></td></tr>";

#############

print "<tr><td align=right><b>".number_format($results['canonicals'])."</b></td><td>Canonical Categories</td><td><a href=\"/breakdown.php?by=canonical\">view breakdown</a></td> <td><small>NOTE: The <a href=\"http://www.geograph.org.uk/stuff/canonical.php\">canonical category database</a> is still being built, <br/>so all categories havnt been give an canonical version yet.</small></td></tr>";

#############

print "</table>";

include($_SERVER['DOCUMENT_ROOT'].'/footer.inc.php');


#########
